﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.Unity;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;

namespace Unity2._1
{
    /// <summary>
    /// 将服务及组件注册到Castle容器
    /// </summary>
    public class BusinessComponentManager
    {
        /// <summary>
        /// 内部的底层容器。
        /// </summary>
        private static IUnityContainer container = new UnityContainer();
        /// <summary>
        /// 有关注册到Castle容器的配置文件完整路径
        /// </summary>
        private static readonly string BusinessXml = AppDomain.CurrentDomain.BaseDirectory + "BusinessComponentManager.xml";
        /// <summary>
        /// 找不到配置文件提示信息
        /// </summary>
        private static readonly string CannotFindBusinessXmlError = "Cannot find " + BusinessXml;

        /// <summary>
        /// 加载配置文件
        /// </summary>
        static BusinessComponentManager()
        {
            if (System.IO.File.Exists(BusinessXml) == true)
            {
                try
                {
                    LoadComponent(BusinessXml);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
            else
            {
                throw new Exception(CannotFindBusinessXmlError);
            }
        }

        /// <summary>
        /// 注册配置文件中服务与组件
        /// </summary>
        /// <param name="xmlName">配置文件的完整文件名</param>
        private static void LoadComponent(string xmlName)
        {
            var map = new ExeConfigurationFileMap { ExeConfigFilename = xmlName };
            System.Configuration.Configuration config
                = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            UnityConfigurationSection section
                = (UnityConfigurationSection)config.GetSection("unity");
            section.Configure(container, "containerOne");
        }

        /// <summary>
        /// 注册单个组件
        /// </summary>
        /// <param name="tFrom">组件的接口例如："Interface.IPageDAO1,Interface"前面是全路径，后面是类库名称</param>
        /// <param name="tTo">组件的实现类例如："DataAccess.PageDAO1,DataAccess"前面是全路径，后面是类库名称</param>
        public static void AddComponent(string tFrom, string tTo)
        {
            //两个参数例如："Interface.IPageDAO1,Interface", "DataAccess.PageDAO1,DataAccess"
            //前面是全路径，后面是类库名称
            try
            {
                container.RegisterType(Type.GetType(tFrom), Type.GetType(tTo));
            }
            catch
            {

            }
        }

        /// <summary>
        /// 注册单个组件
        /// </summary>
        /// <param name="tFrom">组件的接口</param>
        /// <param name="tTo">组件的实现类</param>
        /// <param name="name">key</param>
        public static void AddComponent(string tFrom, string tTo, string name)
        {
            try
            {
                container.RegisterType(Type.GetType(tFrom), Type.GetType(tTo), name);
            }
            catch
            {

            }
        }



        /// <summary>
        /// 取得组件实例
        /// </summary>
        /// <typeparam name="T">实例的接口类</typeparam>
        /// <param name="name">key</param>
        /// <returns>具体实例</returns>
        public static T GetInstance<T>(string name)
        {
            T model = container.Resolve<T>(name);
            return model;
        }

        /// <summary>
        /// 取得组件实例
        /// </summary>
        /// <typeparam name="T">实例的接口类</typeparam>
        /// <returns>具体实例</returns>
        public static T GetInstance<T>()
        {
            T model = container.Resolve<T>();
            return model;
        }
    }
}
